<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<script type="text/javascript" src="<s:url value='/js/pages/sysmaintain/UserDetail.js'/>"></script>

<div class="tempDiv">
<!--jquery validator显示错误信息的容器 -->
 <div class="serverErrorContainer" style="position:relative;">
        <div class="serverError">
        <ol>
        </ol>
        </div>
	</div>
    <!-- end -->

<form id="userForm" class="hForm"   name="form1" action="<s:url value="/user/save"/>?tdqssid=<%=session.getId()%>" method="post">

	
		<s:hidden name="user.userId" id="userId"></s:hidden>
		<div class="rowElem">
			<label>登录名：</label>
			<s:textfield id="username" name="user.loginName" cssClass="dialoginput"></s:textfield>
		</div>
		<div class="rowElem">
			<label>姓名：</label>
			<s:textfield id="username" name="user.userName" cssClass="dialoginput"></s:textfield>
		</div>
		<div class="rowElem">
			<label>密码：</label>
			<s:password id="password" name="user.password" cssClass="dialoginput"></s:password>
		</div>
		<div class="rowElem">
			<label>确认密码：</label>
			<s:password id="password" name="confirmPassWord" cssClass="dialoginput"></s:password>
		</div>
		
		<div class="rowElem">
			<label>座机：</label>
			<s:textfield  id="phone" name="user.phone" cssClass="dialoginput"></s:textfield>
		</div>
		<div class="rowElem">
			<label>手机：</label>
			<s:textfield  id="mobile" name="user.mobile" cssClass="dialoginput"></s:textfield>
		</div>
		<div class="rowElem">
			<label>邮箱：</label>
			<s:textfield  id="email" name="user.email" cssClass="dialoginput"></s:textfield>
		</div>
		
		<table cellspacing="0" cellpadding="0" width="100%" class="listviewTable">
		
			<tr>
				<td class="left">
					<font color="red">*</font>拓扑：
				</td>
				<td >
				<s:textfield readonly="true" id="company"  cssClass="tree" ></s:textfield>
				<s:hidden id="companyId" name="user.topoIds" ></s:hidden>
				</td>
			</tr>
		
			<tr>
				<td align="right">
					<font color="red">*</font>角色：
				</td>
				<td>
					<s:textfield id="user_role_names"  name="user.roleNames" readonly="true"  cssClass="tree"></s:textfield>
					<s:hidden id="user_role_ids" name="user.roleIds" ></s:hidden>
				</td>
			</tr>
		</table>


</form>
</div>
<div id="companyDiv" class="dropDD_contentDiv">
	<div style="overflow:auto;height:266px">
		<ul id="companyTree" class="ztree"></ul>			
	</div>

	<div class="buttonBox" style="position:absolute;bottom:0px;background:#f3f4f4;height:30px;padding-top:4px">
		<a href="javascript:void(0);" class="button" tabindex="-1"><input type="button" onclick="window.curDDWin.saveWin();return false;" value="确认"></a>
		<a href="javascript:void(0);" class="button" tabindex="-1"><input type="button" onclick="clearCompany();return false;" value="清空"></a>
	</div>

</div>

<div id="treepanel" class="dropDD_contentDiv">
	<div style="overflow:auto;height:266px">
		<ul id="tree" class="ztree"></ul>			
	</div>
	<div class="buttonBox" style="position:absolute;bottom:0px;background:#f3f4f4;height:30px;padding-top:4px">
		<a href="javascript:void(0);" class="button" tabindex="-1"><input type="button" onclick="window.curDDWin.saveWin();return false;" value="确认"></a>
		<a href="javascript:void(0);" class="button" tabindex="-1"><input type="button" onclick="clearRoles();return false;" value="清空"></a>
	</div>
</div>
<SCRIPT LANGUAGE="JavaScript">
var treeObject;
var zTreeCompany;
var zTreeRole;
var	settingCompany;
var	settingRole;
var companyNode;

var selectNodeRole;
var selectNodeCompany;
	$(document).ready(
			function() {
				settingRole = {
					check: {
					enable: true,
					chkStyle: "radio",
					radioType: "all"
					},
					asyncParam : ["id"],
					async: {
					enable: true,
					url:"<s:url value='/role/tree'/>",
					autoParam:["id"]
					},
					callback : {
						 onCheck:function(event, treeId, treeNode){
						zTreeRole = $.fn.zTree.getZTreeObj("tree");
						 	var nodes=zTreeRole.getCheckedNodes(true);
						 	var names="";
						 	var ids="";
						 	$.each(nodes,function(i,node){
						 		if(node.checked){
						 			names=names+node.name+" "
						 			ids=ids+node.id
						 			if(i<nodes.length-1){
						 				ids=ids+","
						 			}
						 		}
						 	})
	
						 	$("#user_role_names").val(names);
						 	$("#user_role_ids").val(ids);
						 },
						 // 点击每个节点的事件:
						onAsyncSuccess :function(event, treeId, treeNode, msg) {
							zTreeRole = $.fn.zTree.getZTreeObj("tree");
							zTreeRole = $.fn.zTree.getZTreeObj("tree");;
							if(!selectNodeRole){
								return;
							}
							selectNodeRole = zTreeRole.getNodeByParam("id",selectNodeRole.id, null);
							var pNode = selectNodeRole.getParentNode();
							zTreeRole.expandNode(pNode, true, false, true);
			
							
							if(treeNode!=null){
						  		if(treeNode.nodes.length==0)
						     		treeNode.isParent =false;
						   		else
						     		treeNode.isParent =true;
						 		zTreeRole.updateNode(treeNode, true);
						   }

						}
					}
				};
				
				settingCompany = {
					check: {
					enable: true,
					chkStyle: "radio",
					radioType: "all"
					},
				      	asyncParam : ["id"],
				      	async: {
					enable: true,
					url:"<s:url value='/topo/tree.action'/>",
					autoParam:["id"]
					},
					callback:{
						    onCheck:function(event, treeId, treeNode){
						 	var nodes=zTreeCompany.getCheckedNodes(true);
						 	var names="";
						 	var ids="";
						 	$.each(nodes,function(i,node){
						 		if(node.checked){
						 			names=names+node.name+" "
						 			ids=ids+node.id
						 			if(i<nodes.length-1){
						 				ids=ids+","
						 			}
						 		}
						 	
						 	})
						 	$("#company").val(names);
						 	$("#companyId").val(ids);
						 },
						    onAsyncSuccess :function(event, treeId, treeNode, msg) {
							zTreeCompany = $.fn.zTree.getZTreeObj("companyTree");
							if(!selectNodeCompany){
								return;
							}
							selectNodeCompany = zTreeCompany.getNodeByParam("id",selectNodeCompany.id, null);
							var pNode = selectNodeCompany.getParentNode();
							zTreeCompany.expandNode(pNode, true, false, true);
			
							
							if(treeNode!=null){
						  		if(treeNode.nodes.length==0)
						     		treeNode.isParent =false;
						   		else
						     		treeNode.isParent =true;
						 		zTreeCompany.updateNode(treeNode, true);
						   }

						}
					}
					
				};
				
				$.fn.zTree.init($("#companyTree"), settingCompany);
			});
	

	

	
	function zTreeOnLoadAfter(zTree, msg)
	{	
	  /* zTreeCompany = zTree;
	   zTree.expandAll();
	   var parentid = "<s:property value='company.parent.id'/>";
	   if( parentid != '')	
	   {  
		   var node = zTree.getNodeByParam("id",parentid );             
		   zTree.selectNode(node);
	   }*/
	}
	

	
	$("#company").inputStyle({
		operatorId:"companyMenuBtn",
		operatorType:"text",
		operatorText:"选择",
		controllerWidth:200,
		operatorFun:popcd,
		imgFun:clearCompany
	});

	$("#user_role_names").inputStyle({
			operatorId:"select_roles",
			operatorType:"text",
			operatorText:"选择",
			controllerWidth:200,
			operatorFun:function(){
				poprole();
				$.fn.zTree.init($("#tree"), settingRole);
			},
			imgFun:clearRoles
	});
	
function popcd(){
	new DDWin({
		el:'companyDiv'
		,dep:'divAround_company'
		,clickBody:'save'
	})
}


function poprole(){
	new DDWin({
		el:'treepanel'
		,dep:'divAround_user_role_names'
		,clickBody:'save'
	})
}

function clearCompany(){
	$("#company").val('');
	//$("#departmentId").attr("value","");
	//zTreeCompany = $("#companyTree").zTree(settingCompany);
	//updateDepartmentMenu(null);
}



function clearRoles(){
	$("#user_role_ids").val('');
	$("#user_role_names").val('');
	//zTreeRole = $("#tree").zTree(settingRole);
}




function submitData(){
	if(!$("#userForm").valid()){
				return;
	}

	$.adAjax({
		type : "post",
		url : '<s:url value="/user/save"/>',// 目标地址
		data:$('#userForm').serialize(),
		beforeSend : function() {
			
		},
		complete : function() {

		},
		error : function() {

		},
		success : function(){
			
			reload();
		}
});
}



</script>